/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dressingsocial.service;

import com.dressingsocial.model.Dressing;
import com.dressingsocial.model.Opinion;
import com.dressingsocial.repository.DressingRepository;
import com.dressingsocial.repository.OpinionRepository;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author hayate
 */

@Service
public class OpinionService {

    @Autowired
    OpinionRepository opinionRepository;

    @Autowired
    DressingRepository dressingRepository;

    @Transactional
    public Opinion create(Opinion opinion) {
        Opinion createdOpinion = opinion;
        return opinionRepository.save(createdOpinion);
    }

    @Transactional
    public List<Opinion> getOpinionsOn(Long id) {
        Dressing dressing = dressingRepository.findOne(id);
        if (!dressing.getOpinions().isEmpty()) {
            return dressing.getOpinions();
        } else {
            return new ArrayList<Opinion>();
        }
    }
    
    @Transactional
    public List<Opinion> findHotestOpinions() {
        return opinionRepository.findAll();
    }
}
